package pl.soa.wawek.androidandrest;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import pl.soa.wawek.androidandrest.MyReceiver.Receiver;
import pl.soa.wawek.rest.GetRest;
import pl.soa.wawek.rest.PostRest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginPanel extends Activity implements Receiver{

	TextView header;
	EditText etUserLogin;
	EditText etPasswordLogin;
	EditText etUserRegister;
	EditText etPasswordRegister;
	Button bLogin;
	Button bRegister;
	private JSONObject json;
	private model.User user;
	private int codeFromService;
	private MyReceiver mReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_panel);
		mReceiver = new MyReceiver(new Handler());
		mReceiver.setReceiver(this);
		
		header = (TextView) findViewById(R.id.tvUser);
		etUserLogin = (EditText) findViewById(R.id.etUserLogin);
		etPasswordLogin = (EditText) findViewById(R.id.etPasswordLogin);
		etUserRegister = (EditText) findViewById(R.id.etUserRegister);
		etPasswordRegister = (EditText) findViewById(R.id.etPasswordRegister);
		bLogin = (Button) findViewById(R.id.bLogin);
		bRegister = (Button) findViewById(R.id.bRegister);

		bRegister.setText("Zarejestruj");
		bLogin.setText("Zaloguj");

		bLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				json = new JSONObject();
				// checkPassword(etPasswordLogin.getText().toString());
				user = new model.User(0, etUserLogin.getText().toString(),
						etPasswordLogin.getText().toString(), true);
				try {
					json.put("id", user.getId());
					json.put("nick", user.getNick());
					json.put("password", user.getPassword());
					json.put("registered", user.isRegistered());
				} catch (JSONException e) {
					e.printStackTrace();
				}
				Intent serviceIntent = new Intent(LoginPanel.this,
						PostRest.class);
				serviceIntent.putExtra("receiver", mReceiver);
				serviceIntent.putExtra("jsonobj", json.toString());
				serviceIntent.putExtra("class", "user"); // zeby odpowiednie
															// trafialo do
															// sciezki adresu
				startService(serviceIntent);
				onReceiveResult(0,new Bundle()); //watpie ale warto sprawdzic
				Log.i("taag", Integer.toString(codeFromService));
				// sprawdzenie czy logowanie sie powiodlo
				/*
				 * if (codeFromService > 0) {
				 * 
				 * Intent getService = new Intent(LoginPanel.this,
				 * GetRest.class); getService.putExtra("class", "conference");
				 * startService(getService); GetRest gr = new GetRest();
				 * ArrayList<model.Conference> conf = gr.getConferences();
				 * Intent i = new Intent(LoginPanel.this, MainActivity.class);
				 * i.putParcelableArrayListExtra("conferences", conf);
				 * startActivity(i);
				 * 
				 * }
				 */
			}

		});

		bRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				json = new JSONObject();
				// checkPassword(etPasswordLogin.getText().toString());
				user = new model.User(0, etUserRegister.getText()
						.toString(), etPasswordRegister.getText().toString(),
						false);
				try {
					json.put("id", user.getId());
					json.put("nick", user.getNick());
					json.put("password", user.getPassword());
					json.put("registered", user.isRegistered());
				} catch (JSONException e) {
					e.printStackTrace();
				}
				Intent serviceIntent = new Intent(LoginPanel.this,
						PostRest.class);
				serviceIntent.putExtra("receiver", mReceiver);
				serviceIntent.putExtra("jsonobj", json.toString());
				serviceIntent.putExtra("class", "user"); // zeby odpowiednie
															// trafialo do
															// sciezki adresu
				startService(serviceIntent);
				
			}

		});
	}

	public boolean checkPassword(String password) {
		if (password.length() < 6) {
			Toast t = new Toast(LoginPanel.this);
			t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
			t.setDuration(Toast.LENGTH_SHORT);
			t.setText("Haslo musi zawierac przynajmniej 6 znakow");
			t.show();
			return false;
		} else if (password.contains(" ")) {
			Toast t = new Toast(LoginPanel.this);
			t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
			t.setDuration(Toast.LENGTH_SHORT);
			t.setText("Haslo nie moze zawierac spacji");
			t.show();
			return false;
		}
		return true;
	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		// TODO Auto-generated method stub
		this.codeFromService = resultData.getInt("code");
	}
}
